//
//  CJNetworkManager+Extensions.swift
//  SwiftDemo
//
//  Created by Chris on 2017/6/27.
//  Copyright © 2017年 chris. All rights reserved.
//

import Foundation

extension CJNetworkManager {
    
    /// 获取statusjson数据
    ///
    /// - Parameter completion: 完成回调[json数组, 是否获取到数据]
    class func statusList(since_id: Int = 0, max_id: Int = 0, completion: @escaping (_ lists: [[String: Any]]?, _ isSuccess: Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parameters = ["since_id": "\(since_id)", "max_id": "\(max_id > 0 ? (max_id - 1) : 0)"]
        
        sharedManager.tokenRequest(URLString: urlString, parameters: parameters) { (json, isSuccess) in
            let result = (json as? [String : Any])?["statuses"] as? [[String: Any]]
            completion(result, isSuccess)
        }
    }
    
    /// 获取未读微博数量
    ///
    /// - Parameter completion: 获取数量成功后回调(数量)
    class func unreadCount(completion: @escaping (Int) -> ()) {
        
        guard let uid = sharedManager.userAccount.uid else {
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let parameters = ["uid": uid]
        
        sharedManager.tokenRequest(URLString: urlString, parameters: parameters) { (json, isSuccess) in
            
            let result = json as? [String: Any]
            
            let count = result?["status"] as? Int ?? 0
            
            completion(count)
        }
        
    }
    
}
